<script type="text/javascript"> 
	Yutil.useLoadingMessage();
	Yutil.en.enhance();
</script>    
<%
	String employeeIds = request.getParameter("employeeids");
	//if trans the enterprise id, different process
	String[]  affectedEmployee = employeeIds.split(";");
	
	String[] employeeIdArr = new String[affectedEmployee.length];
	for(int i = 0; i < employeeIdArr.length; i++) {
		employeeIdArr[i] = affectedEmployee[i].substring(1);
		if(! affectedEmployee[i].startsWith("E")) {
				throw new Exception("Is not employee");
		}
	}
%>
<fieldset>
	<legend>当前所计算的人</legend>
	<table align="left">
		<tr>
			<td>
				<textarea disabled id="effectedEmpls"/><%= employeeIds%></textarea>
			</td>
		</tr>
	</table>
</fieldset>

<fieldset class="form">
	<legend>四金计算</legend>
	<table align="left" id="service_Enterprise_create_form">
		<tr>
			<td>
				<!-- 
				Hi mengc, I don't know why here need the name attribute " ' ",but not " " ".
				When I use ", there will be one defect.
				 -->
				开始日期：<input id="salaryServiceStartDate" type="text" name='salaryServiceStartDate' value="" class="date"/><br/>
			</td>
		</tr>
		<tr>
			<td>
				结束日期：<input id="salaryServiceEndDate" type="text" name='salaryServiceEndDate' value="" class="date"/><br/>
			</td>
		</tr>
	</table>
	<br/><p></p>
	<div class="hrule-dots"></div>
	<!-- 基本工资与缴纳基数 -->
	<table align="left">
		<tr>
			<th>基本工资</th>
			<td><input id="baseWage" name="baseWage" class="number" onkeyup="calculateFormValue(this)"/></td>
		</tr>
		<tr>
			<th>保险金基数</th>
			<td><input id="insuranceBase" name="insuranceBase" class="number" onkeyup="calculateFormValue(this)"/></td>
		</tr>
		<tr>
			<th>公积金基数</th>
			<td><input id="housingfundBase" name="housingfundBase" class="number" onkeyup="calculateFormValue(this)"/></td>
		</tr>
		<!-- some inputs need child page input -->
		<input type="hidden" id="insurance_value"/>
		<input type="hidden" id="housingfund_value"/>
		<input type="hidden" id="incometax_value"/>
		<tr>
			<td colspan="2">
				<p class="info" align="left">To calculate the after tax salary: You need click the "保险金计算" first, click "公积金计算" then, and click "个人所得税计算" last.</p>
			</td>
		</tr>
	</table>
	<div class="hrule-dots"></div>
	<p></p>
	<br/>
	<%
		String[] insurancePars = new String[1];
		insurancePars[0] = "'insuranceEmployees=" + employeeIds + "'";
	%>
	<br/>
	<div class="tabs"> 
		<a onclick="var myAjax = new Ajax.Updater('financial_div','f_insurance_calculate.jsp' ,{method:'get', evalScripts: true});" href="javascript:;">保险金计算</a>
		<a onclick="var myAjax = new Ajax.Updater('financial_div','f_housingfund_calculate.jsp' ,{method:'get', evalScripts: true});" href="javascript:;">公积金计算</a>
		<a onclick="var myAjax = new Ajax.Updater('financial_div','f_incometax_calculate.jsp' ,{method:'get', evalScripts: true});" href="javascript:;">个人所得税计算</a>
	</div> 
	<div id="financial_div">
		<%@include file="/f_insurance_calculate.jsp" %>
	</div>
	
	<p>
		<!-- 对应存入service表 -->
		<input class='button' type="button" value="保存" onclick="saveEnterpriseFinance(this)"/>
		<input class='button' type="button" value="返回" onclick="actionPage('la_enterprise_list.jsp')"/>
	</p>
</fieldset>

<script type="text/javascript">
	// Jetty Yang  
	//    distract: dight要格式化的数字，mun要保留的小数位数。  
	distract = function(digit, num)  
	{  
		digit  =  Math.round(digit * Math.pow(10, num)) / Math.pow(10,num);  
        return  digit;  
	}  

	calculateFormValue = function(oElem) {
		if(!Yform.validate(oElem))
			return;
		var baseWage = $('baseWage').value;
		//calculate base
		$('insuranceBase').value = baseWage;
		$('housingfundBase').value = baseWage;
		
		//calculate insurance
		calculateInsurance();
	}
	
	//calculateInsurance
	calculateInsurance = function() {
		var baseWage = $('insuranceBase').value;
		
		//enterprise
		$('endowment_enterprise').value = distract(baseWage * parseInt($('endowment_enterprise_rate').value) / 100, 2);
		$('medical_enterprise').value = distract(baseWage * parseInt($('medical_enterprise_rate').value) / 100, 2);
		$('suspension_enterprise').value = distract(baseWage * parseInt($('suspension_enterprise_rate').value) / 100, 2);
		$('workplace_enterprise').value = distract(baseWage * Number($('workplace_enterprise_rate').value) / 100, 2);
		$('maternity_enterprise').value = distract(baseWage * Number($('maternity_enterprise_rate').value) / 100, 2);
		
		//employee
		$('endowment_employee').value = distract(baseWage * parseInt($('endowment_employee_rate').value) / 100, 2);
		$('medical_employee').value = distract(baseWage * parseInt($('medical_employee_rate').value) / 100, 2);
		$('suspension_employee').value = distract(baseWage * parseInt($('suspension_employee_rate').value) / 100, 2);
		
		$('insurance_value').value = Number($('endowment_employee').value) 
								   + Number($('medical_employee').value)
								   + Number($('suspension_employee').value);
	}
	
	calculateHousingfund = function() {
		var baseWage = $('housingfundBase').value;
		
		//enterprise
		$('housingfund_enterprise').value = distract(baseWage * parseInt($('housingfund_enterprise_rate').value) / 100, 2);
		
		//employee
		$('housingfund_employee').value = distract(baseWage * parseInt($('housingfund_employee_rate').value) / 100, 2);
		
		$('housingfund_value').value = Number($('housingfund_employee').value);
	}
	
	calculateIncometax = function() {
		
	}
	
	saveEnterpriseFinance = function(btn) {
		var url = "/" + "<%= LocalContants.PROJECT_NAME %>" + "/serviceEnterpriseCreate.do";
		var pars = Form.serialize($('service_Enterprise_create_form'));
		pars = pars + '&employeeids=' + $('effectedEmpls').value;
		pars = pars + '&baseWage=' + $('baseWage').value;
		pars = pars + '&insuranceBase=' + $('insuranceBase').value;
		pars = pars + '&housingfundBase=' + $('housingfundBase').value;
			var myAjax = new Ajax.Updater("right",url,{method:'post',parameters: pars, evalScripts:true});
		//if(Yform.validateAllOnSubmit(btn)) {
		//}	
	}
</script>

<%@ page import="com.esms.struts.service.*" %>
<%@ page language="java" contentType="text/html; charset=utf-8" %>